package com.zhuoyue.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.zhuoyue.common.annotation.DataScope;
import com.zhuoyue.common.core.domain.CategoryNameAndNum;
import com.zhuoyue.common.utils.DateUtils;
import com.zhuoyue.system.domain.TblDevice;
import com.zhuoyue.system.domain.vo.JointControlRelationVo;
import com.zhuoyue.system.domain.vo.TblDeviceVo;
import com.zhuoyue.system.mapper.CategoryNameCodeMapper;
import com.zhuoyue.system.mapper.TblDeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhuoyue.system.mapper.JointControlRelationMapper;
import com.zhuoyue.system.domain.JointControlRelation;
import com.zhuoyue.system.service.IJointControlRelationService;

/**
 * 多联多控Service业务层处理
 *
 * @author zyzn
 * @date 2021-09-24
 */
@Service
public class JointControlRelationServiceImpl implements IJointControlRelationService {
    @Autowired
    private JointControlRelationMapper jointControlRelationMapper;
    @Autowired
    private TblDeviceMapper deviceMapper;
    @Autowired
    private CategoryNameCodeMapper categoryNameCodeMapper;

    /**
     * 查询多联多控
     *
     * @param id 多联多控ID
     * @return 多联多控
     */
    @Override
    public JointControlRelation selectJointControlRelationById(String id) {
        return jointControlRelationMapper.selectJointControlRelationById(id);
    }

    /**
     * 查询多联多控列表
     *
     * @param jointControlRelation 多联多控
     * @return 多联多控
     */
    @DataScope(deptAlias = "s_d")
    @Override
    public List<JointControlRelationVo> selectJointControlRelationList(JointControlRelation jointControlRelation) {

        List<JointControlRelationVo> jointControlRelations = jointControlRelationMapper.
                selectJointControlRelationList(jointControlRelation);

        // 查询主动设备和被动设备的设备类型名称
        List<CategoryNameAndNum> categoryNameAndNumList = categoryNameCodeMapper.getCategoryNameAndNumList();
        boolean flag1, flag2;
        for (JointControlRelationVo controlRelation : jointControlRelations) {
            flag1 = false;
            flag2 = false;
            for (CategoryNameAndNum categoryNameAndNum : categoryNameAndNumList) {
                if (categoryNameAndNum.getCategoryNum().equals(controlRelation.getStartDeviceType())) {
                    controlRelation.setStartDeviceCategoryName(categoryNameAndNum.getCategoryName());
                    flag1 = true;
                }
                if (categoryNameAndNum.getCategoryNum().equals(controlRelation.getEndDeviceType())) {
                    controlRelation.setEndDeviceCategoryName(categoryNameAndNum.getCategoryName());
                    flag2 = true;
                }

                if (flag1 && flag2) {
                    break;
                }
            }
        }

        return jointControlRelations;
    }

    @Override
    public List<JointControlRelationVo> exportJointControlRelationList(JointControlRelation jointControlRelation) {
        return jointControlRelationMapper.selectJointControlRelationList(jointControlRelation);
    }

    /**
     * 新增多联多控
     *
     * @param jointControlRelation 多联多控
     * @return 结果
     */
    @Override
    public int insertJointControlRelation(JointControlRelation jointControlRelation) {
        jointControlRelation.setCreateTime(DateUtils.getNowDate());
        return jointControlRelationMapper.insertJointControlRelation(jointControlRelation);
    }

    /**
     * 修改多联多控
     *
     * @param jointControlRelation 多联多控
     * @return 结果
     */
    @Override
    public int updateJointControlRelation(JointControlRelation jointControlRelation) {
        return jointControlRelationMapper.updateJointControlRelation(jointControlRelation);
    }

    /**
     * 批量删除多联多控
     *
     * @param ids 需要删除的多联多控ID
     * @return 结果
     */
    @Override
    public int deleteJointControlRelationByIds(String[] ids) {
        return jointControlRelationMapper.deleteJointControlRelationByIds(ids);
    }

    /**
     * 删除多联多控信息
     *
     * @param id 多联多控ID
     * @return 结果
     */
    @Override
    public int deleteJointControlRelationById(String id) {
        return jointControlRelationMapper.deleteJointControlRelationById(id);
    }
}
